﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

using XianShang.Data;
using XianShang.Entity;
using XianShang.Common;
namespace XianShang.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        /// <summary>
        /// 增加一條新聞信息（來源為空時）
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddNewsInfo(NewsInfo Ninfo)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@NewsTitle",(DbType)SqlDbType.NVarChar,300,Ninfo.NewsTitle),
                                       DbHelper.MakeInParam("@NewsContent",(DbType)SqlDbType.NText,1073741823,Ninfo.NewsContent),
                                       DbHelper.MakeInParam("@NewsTimes",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,Ninfo.NewsTimes),
                                       DbHelper.MakeInParam("@NewsAdminId",(DbType)SqlDbType.Int,int.MinValue,Ninfo.NewsAdminId)
                                   };
            string commandText = "INSERT INTO [dbo].[XS_News]([NewsTitle],[NewsContent],[NewsFrom],[NewsHits],[NewsTimes],[NewsAdminId])VALUES(@NewsTitle,@NewsContent,'遊戲網站',0,@NewsTimes,@NewsAdminId)";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }

        /// <summary>
        /// 增加一條新聞信息（來源不為空時）
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddNewsInfo1(NewsInfo Ninfo)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@NewsTitle",(DbType)SqlDbType.NVarChar,300,Ninfo.NewsTitle),
                                       DbHelper.MakeInParam("@NewsContent",(DbType)SqlDbType.NText,1073741823,Ninfo.NewsContent),
                                       DbHelper.MakeInParam("@NewsFrom",(DbType)SqlDbType.NVarChar,100,Ninfo.NewsFrom),
                                       DbHelper.MakeInParam("@NewsTimes",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,Ninfo.NewsTimes),
                                       DbHelper.MakeInParam("@NewsAdminId",(DbType)SqlDbType.Int,int.MinValue,Ninfo.NewsAdminId)
                                   };
            string commandText = "INSERT INTO [dbo].[XS_News]([NewsTitle],[NewsContent],[NewsFrom],[NewsHits],[NewsTimes],[NewsAdminId])VALUES(@NewsTitle,@NewsContent,@NewsFrom,0,@NewsTimes,@NewsAdminId)";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }

        /// <summary>
        /// 修改一條新聞信息（來源為空時）
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int UpNewsInfo(string NewsTitle, string NewsContent, string NewsTimes, string Id)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@NewsTitle",(DbType)SqlDbType.NVarChar,300,NewsTitle),
                                       DbHelper.MakeInParam("@NewsContent",(DbType)SqlDbType.NText,1073741823,NewsContent),
                                       DbHelper.MakeInParam("@NewsTimes",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,NewsTimes),
                                       DbHelper.MakeInParam("@Id",(DbType)SqlDbType.Int,int.MinValue,Id)
                                   };
            string commandText = "UPDATE [dbo].[XS_News] SET [NewsTitle]=@NewsTitle,[NewsContent]=@NewsContent,[NewsFrom]='游戲網站',[NewsTimes]=@NewsTimes where [ID]=@Id";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }

        /// <summary>
        /// 修改一條新聞信息（來源不為空時）
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int UpNewsInfo1(string NewsTitle, string NewsContent, string NewsFrom, string NewsTimes, string Id)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@NewsTitle",(DbType)SqlDbType.NVarChar,300,NewsTitle),
                                       DbHelper.MakeInParam("@NewsContent",(DbType)SqlDbType.NText,1073741823,NewsContent),
                                       DbHelper.MakeInParam("@NewsFrom",(DbType)SqlDbType.NVarChar,100,NewsFrom),
                                       DbHelper.MakeInParam("@NewsTimes",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,NewsTimes),
                                       DbHelper.MakeInParam("@Id",(DbType)SqlDbType.Int,int.MinValue,Id)
                                   };
            string commandText = "UPDATE [dbo].[XS_News] SET [NewsTitle]=@NewsTitle,[NewsContent]=@NewsContent,[NewsFrom]=@NewsFrom,[NewsTimes]=@NewsTimes where [ID]=@Id";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }

        
        /// <summary>
        /// 獲取信息顯示新聞列表
        /// </summary>
        /// <returns></returns>
        public IDataReader GetNewsInfo()
        {
            string commandText = "SELECT * FROM [dbo].[XS_News] order by [newstimes] desc";
            return DbHelper.ExecuteReader(CommandType.Text, commandText);
        }
        /// <summary>
        /// 獲取一條要修改的新聞信息顯示
        /// </summary>
        /// <returns></returns>
        public NewsInfo GetNewsId(string NewId)
        {
            DbParameter getPar = DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int, int.MaxValue, NewId);
            string commandText = "SELECT * FROM [dbo].[XS_News] WHERE [Id]=@Id";
             NewsInfo inf = null;
            DataSet ds=DbHelper.ExecuteDataset(CommandType.Text, commandText, getPar);
            if (ds.Tables[0].Rows.Count > 0)
            {
                inf = new NewsInfo();
                inf.ID = Utils.objToInt64(ds.Tables[0].Rows[0]["id"], -1);
                inf.NewsTitle = Utils.objToStr(ds.Tables[0].Rows[0]["NewsTitle"]);
                inf.NewsFrom = Utils.objToStr(ds.Tables[0].Rows[0]["NewsFrom"]);
                inf.NewsTimes = Utils.objToStr(ds.Tables[0].Rows[0]["NewsTimes"]);
                inf.NewsContent = Utils.objToStr(ds.Tables[0].Rows[0]["NewsContent"]);
            }
            return inf;
        }

        /// <summary>
        /// 根據新聞ID刪除新聞信息
        /// </summary>
        /// <returns></returns>
        public int DelNewsInfo(string NewId)
        {
            DbParameter getPar = DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int, int.MaxValue, NewId);
            string commandText = "DELETE FROM [dbo].[XS_News] WHERE [Id]=@Id";

            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar);
        }

        /// <summary>
        /// 更新新聞點擊量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpNewsHits(string id)
        {
            DbParameter getPar = DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int, int.MaxValue, id);
            string commandText = "UPDATE [dbo].[XS_News] SET [NewsHits] =[NewsHits]+1 WHERE [Id]=@id";
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar);
        }

    }
}
